#include "widget.h"
#include "ui_widget.h"
#include "messagepackage.h"
#include "sockethelper.h"
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->error_lab->hide();



    this->socket_helper = socketHelper::getInstance();
    this->socket=this->socket_helper->getSocket();

    this->my_mainWindow=new MainWindow(this->socket,this);
    this->my_mainWindow->hide();

    connect(this->socket_helper,&socketHelper::registSuccess,this,&Widget::on_registSuccess);
    connect(this->socket_helper,&socketHelper::registFailed,this,&Widget::on_registFailed);
    connect(this->socket_helper,&socketHelper::loginSuccess,this,&Widget::on_loginSuccess);
    connect(this->socket_helper,&socketHelper::loginSuccess,this->my_mainWindow,&MainWindow::setClient_user);
    connect(this->socket_helper,&socketHelper::add_onl,this->my_mainWindow,&MainWindow::update_onlUser);
    connect(this->socket_helper,&socketHelper::recv_singleMessage,this->my_mainWindow,&MainWindow::on_recvSingleMessage);
    connect(this->socket_helper,&socketHelper::loginFailed,this,&Widget::on_loginFailed);

    connect(this->my_mainWindow,&MainWindow::mainQuit,this,&Widget::on_mainQuit);//退出后显示登录

    connect(ui->pswdInput,&QLineEdit::textEdited,this,&Widget::hide_errorLab);
    connect(ui->nameInput,&QLineEdit::textEdited,this,&Widget::hide_errorLab);

    //组相关
    connect(socket_helper, &socketHelper::groupListReceived, my_mainWindow, &MainWindow::on_groupListReceived);
    connect(socket_helper, &socketHelper::groupMessageReceived, my_mainWindow, &MainWindow::on_groupMessageReceived);
    connect(socket_helper, &socketHelper::groupJoined, my_mainWindow, &MainWindow::on_groupJoined);
    connect(socket_helper, &socketHelper::groupMemberAdded, my_mainWindow, &MainWindow::on_groupMemberAdded);
    connect(socket_helper, &socketHelper::groupCreated, my_mainWindow, &MainWindow::onGroupCreated);

    //文件相关
    connect(socket_helper, &socketHelper::fileListReceived, my_mainWindow, &MainWindow::onFileListReceived);
    connect(socket_helper, &socketHelper::fileDataReceived, my_mainWindow, &MainWindow::onFileDataReceived);
    connect(socket_helper, &socketHelper::fileUploadSuccess, my_mainWindow, &MainWindow::onFileUploadSuccess);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_regist_PB_clicked()
{
    this->name = ui->nameInput->text();
    this->pswd = ui->pswdInput->text();

    if(this->name.isEmpty() || this->pswd.isEmpty())
    {
        return ;
    }
    MessagePackage pack;
    pack.setType("regist");
    pack.setUserName(this->name);
    pack.setUserPswd(this->pswd);
    MessagePackage::sendMessage(pack,this->socket);
}

void Widget::on_registSuccess()
{
    QMessageBox::information(this,"注册","注册成功");
}

void Widget::on_registFailed()
{
    QMessageBox::warning(this,"注册","注册失败");
}

void Widget::on_login_PB_clicked()
{
    this->name = ui->nameInput->text();
    this->pswd = ui->pswdInput->text();

    if(this->name.isEmpty() || this->pswd.isEmpty())
    {
        return ;
    }
    MessagePackage pack;
    pack.setType("login");
    pack.setUserName(this->name);
    pack.setUserPswd(this->pswd);
    MessagePackage::sendMessage(pack,this->socket);
}

void Widget::on_loginSuccess()
{
    socket_helper->setCurrentUser(name);
    this->my_mainWindow->show();
    this->hide();
}

void Widget::on_loginFailed()
{
    ui->error_lab->show();
}

void Widget::on_mainQuit()
{
    this->show();
    this->my_mainWindow->hide();
    MessagePackage pack;
    pack.setType("quit");
    pack.setUserName(this->name);
    pack.setUserPswd(this->pswd);
    MessagePackage::sendMessage(pack,this->socket);
}

void Widget::hide_errorLab()
{
    ui->error_lab->hide();
}


